# -*- encoding: utf-8 -*-

# Copyright 2010 István Szentandrási
# <etude-music-player@googlegroups.com>

# This file is part of Etude.
#
# Etude is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as 
# published by the Free Software Foundation, either version 3 
# of the License, or (at your option) any later version.
#
# Etude is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Etude. If not, see <http://www.gnu.org/licenses/>.

import gtk
from .. import preferences
from .. import client
from collview import *
from minfo import *
from filechooser import *

class ProfileChooser(gtk.Dialog):
    """
    The profile chooser dialog - if the default profile could not be
    loaded this is shown
    """

    def __init__(self, parent, control):
        gtk.Dialog.__init__(self, title = _("Profile chooser"), parent = parent,
                flags = gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                buttons=(gtk.STOCK_CONNECT, gtk.RESPONSE_OK, gtk.STOCK_QUIT, gtk.RESPONSE_CLOSE
                    )
                )
        self.control = control
        self.prof_frame = preferences.ProfilesFrame(self.control, connect_button = False)
        self.vbox.pack_start(self.prof_frame, True, True, 3)
        #self.set_default_size(300, 350)
        self.vbox.show_all()

def get_property_combo(active = '', translate = False):
    def add(prop, func):
        if translate:
            func((prop, _(prop)))
        else:
            func((prop,))
    if translate:
        store = gtk.ListStore(str, str)
    else:
        store = gtk.ListStore(str)
    active_index = -1
    for property in client.PROP_SORT_SEARCH:
        add(property, store.append)
        if active == property:
            active_index = client.PROP_SORT_SEARCH.index(active)
    combo = gtk.ComboBoxEntry(store, column= int(translate))
    if active_index>=0:
        combo.set_active(active_index)
    elif active:
        add(active, store.prepend)
        combo.set_active(0)
    return combo

def force_image_on_button(widget, show=True):
    # @FIXME reeeealy ugly hack for gnome 2.28 to show the icon
    try:
        widget.get_child().get_child().get_children()[0].props.visible = show
    except IndexError:
        pass

def force_no_label_on_button(widget, hide=True):
    try:
        widget.get_child().get_child().get_children()[1].props.visible = not hide
    except IndexError:
        pass

def force_image_on_button_settings():
    settings = gtk.settings_get_default()
    settings.set_long_property('gtk-button-images', 1, 'Etude')

def format_button(button, no_label=False, force_image=False, relief=gtk.RELIEF_NORMAL,
        image=None, stock_image=None):
    if image:
        button.set_image(image)
    if stock_image:
        button.set_image(
                gtk.image_new_from_stock(stock_image, gtk.ICON_SIZE_BUTTON)
                )
    button.set_relief(relief)
    if no_label:
        force_no_label_on_button(button, no_label)
    if force_image:
        force_image_on_button(button, force_image)

def about_dialog():
    dialog = gtk.AboutDialog()
    dialog.set_name("Etude")
    dialog.set_version("0.1alpha")
    dialog.set_copyright("© Szentandrási István")
    dialog.set_authors(["Szentandrási István","","Thanks for patches from:",
        "jarryson", "Thomas Jollans (tjol)"])
    dialog.set_license("""LGPL
Etude is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Foobar.  If not, see <http://www.gnu.org/licenses/>.""")
    pixbuf= gtk.icon_theme_get_default().load_icon("etude", 64, 0)
    dialog.set_logo(pixbuf)
    dialog.set_website("http://code.google.com/p/etude-music-player/")
    dialog.set_website_label(_("Project home"))
    dialog.set_translator_credits("jarryson")
    dialog.run()
    dialog.destroy()

#from quodlibet, thx ;)
class RPaned(object):
    """A Paned that supports relative (percentage) width/height setting."""

    def get_relative(self):
        """Return the relative position of the separator, [0..1]."""
        if self.get_property('max-position') > 0:
            return float(self.get_position())/self.get_property('max-position')
        else: return 0.75

    def set_relative(self, v):
        """Set the relative position of the separator, [0..1]."""
        return self.set_position(int(v * self.get_property('max-position')))

class RHPaned(RPaned, gtk.HPaned): pass
class RVPaned(RPaned, gtk.VPaned): pass

